package org.example.controller;

import org.example.domain.FruitInbound;
import org.example.response.ApiResponse;
import org.example.service.FruitInboundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/inbound")
public class FruitInboundController {

    @Autowired
    private FruitInboundService fruitInboundService;

    /**
     * 新增入库单
     */
    @PostMapping("/create")
    public ApiResponse<String> createInbound(@RequestBody FruitInbound inbound) {
        try {
            // 计算总金额
            inbound.setTotalAmount(inbound.getQuantity().multiply(inbound.getPrice()));
            int saved = fruitInboundService.createInbound(inbound);
            return ApiResponse.success("创建成功");
        } catch (Exception e) {
            return ApiResponse.error(500, "创建入库单失败：" + e.getMessage());
        }
    }

    /**
     * 获取入库单列表
     */
    @GetMapping("/list")
    public ApiResponse<List<FruitInbound>> getInboundList(
            @RequestParam(required = false) String fruitName,
            @RequestParam(required = false) String supplierName,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            List<FruitInbound> list = fruitInboundService.getInboundList(fruitName, supplierName, startTime, endTime, pageNum, pageSize);
            return ApiResponse.success(list);
        } catch (Exception e) {
            return ApiResponse.error(500, "获取入库单列表失败：" + e.getMessage());
        }
    }

    /**
     * 获取入库单详情
     */
    @GetMapping("/{id}")
    public ApiResponse<FruitInbound> getInboundDetail(@PathVariable Long id) {
        try {
            FruitInbound inbound = fruitInboundService.getInboundById(id);
            return ApiResponse.success(inbound);
        } catch (Exception e) {
            return ApiResponse.error(500, "获取入库单详情失败：" + e.getMessage());
        }
    }

    /**
     * 审核入库单
     * @param id 入库单ID（从路径中获取）
     * @param request 请求体，包含status参数
     * @return 审核结果
     */
    @PostMapping("/audit/{id}")
    public ApiResponse<String> auditInbound(@PathVariable Long id, @RequestBody Map<String, String> request) {
        String status = request.get("status");

        // 根据status值执行不同的操作
        if ("1".equals(status)) {
            // 审核通过
            fruitInboundService.approveInbound(id);
            return ApiResponse.success("审核通过");
        } else if ("2".equals(status)) {
            // 拒绝入库单
            fruitInboundService.rejectInbound(id);
            return ApiResponse.success("拒绝入库通过");
        } else {
            return ApiResponse.error(500, "错误的状态值");
        }
    }

    /**
     * 删除入库单
     */
    @DeleteMapping("/{id}")
    public ApiResponse<Void> deleteInbound(@PathVariable Long id) {
        try {
            fruitInboundService.deleteInbound(id);
            return ApiResponse.success(null);
        } catch (Exception e) {
            return ApiResponse.error(500, "删除入库单失败：" + e.getMessage());
        }
    }
}
